package com.ming_za.odupdaterserver.core.sqlite.type_handler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ming_za.odupdaterserver.core.utils.JsonUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@MappedTypes(List.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
@Component
public class MainUpdateItemListTypeHandler extends JsonTypeHandler<List<Long>>{

    public MainUpdateItemListTypeHandler(JsonUtils jsonUtils) {
        super(jsonUtils);
    }

    @Override
    public List<Long> toObj(String json) {
        try {
            return new ArrayList<>(Arrays.stream(jsonUtils.fromJson(json, Long[].class)).toList());
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
